package useredis

import (
	"fmt"
	"myOJ/db"

	"encoding/json"

	"context"

	"github.com/go-redis/redis/v8"
)

var redisDB *redis.Client
var ctx = context.Background()

func updateHomePageProblems() {
	problems := db.Query()
	input, _ := json.Marshal(problems)
	redisDB.Set(ctx, "homePageProblems", input, 0)
}

func init() {
	redisDB = redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
	})
	updateHomePageProblems()
}

func QueryHomePageProblems() map[string]string {
	var homePageproblems map[string]string

	val, err := redisDB.Get(ctx, "homePageProblems").Result()
	if err != nil {
		fmt.Println("redis get nothing.", err)
	}

	err = json.Unmarshal([]byte(val), &homePageproblems)
	if err != nil {
		fmt.Println(err)
	}
	return homePageproblems
}
